// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import com.google.gdata.util.ServiceException;
import com.google.visualization.datasource.DataSourceServlet;
import com.google.visualization.datasource.base.TypeMismatchException;
import com.google.visualization.datasource.datatable.ColumnDescription;
import com.google.visualization.datasource.datatable.DataTable;
import com.google.visualization.datasource.datatable.value.ValueType;
import com.google.visualization.datasource.query.Query;
import com.ual.dashboard.Fusion;
import com.ual.dashboard.Server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimeZone;

import javax.servlet.http.HttpServletRequest;

/**
 * A demo servlet for serving a simple, constant data table.
 * This servlet extends DataSourceServlet.
 *
 * @author Nimrod T.
 */
public class SimpleExampleServlet extends DataSourceServlet {

  @Override
  public DataTable generateDataTable(Query query, HttpServletRequest request) {
    // Create a data table.
	  Calendar calendar = Calendar.getInstance();
	  calendar.setTimeZone(TimeZone.getTimeZone("CST"));
	  	int month = calendar.get(Calendar.MONTH) + 1;
	  	int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
	  	int hour_of_the_day = calendar.get(Calendar.HOUR_OF_DAY);
	  	int minute = calendar.get(Calendar.MINUTE);
	
	  String date = month + "/" + dayOfMonth + "/2010";
	  System.out.println(date);
	  ArrayList  rowids = null;
	 
    DataTable data = new DataTable();
    ArrayList<ColumnDescription> cd = new ArrayList<ColumnDescription>();
    cd.add(new ColumnDescription("Server", ValueType.TEXT, "Server"));
    cd.add(new ColumnDescription("Asrtimeout", ValueType.TEXT, "Asrtimeout"));
    cd.add(new ColumnDescription("networktimeout", ValueType.TEXT, "networktimeout"));
    cd.add(new ColumnDescription("samplefailure", ValueType.TEXT, "samplefailure"));
    cd.add(new ColumnDescription("webIssueXmlTimOut", ValueType.TEXT, "webIssueXmlTimOut"));
    cd.add(new ColumnDescription("teleSvrRejCalls", ValueType.TEXT, "teleSvrRejCalls"));
    cd.add(new ColumnDescription("asrerrorresponse", ValueType.TEXT, "asrerrorresponse"));
    cd.add(new ColumnDescription("cnHLMOutOfService", ValueType.TEXT, "cnHLMOutOfService"));
    cd.add(new ColumnDescription("defaultTrap", ValueType.TEXT, "defaultTrap"));
    /*cd.add(new ColumnDescription("networktimeout", ValueType.NUMBER, "networktimeout"));
    cd.add(new ColumnDescription("sampleerror", ValueType.NUMBER, "sampleerror"));*/

    data.addColumns(cd);

    // Fill the data table.
    try {
    	  Fusion example = new Fusion("h","h");
		  rowids = example.runSelect("select server,asrtimeout,cnNetworkTimeout," +
		  		"samp_fail_on_web_server,websphere_issue_xml_timeout," +
		  		"teleserverRejCalls,asrerrorresponse,cnHLMOutOfService,defaultTrap" +
		  		" from 207767 where date = '" + date + "'" );
		  Object[] elements = rowids.toArray();
		   	 Server fusionTable = null;
		  for(int i=0; i < elements.length ; i++){
			   fusionTable = (Server)elements[i];
			   //System.out.println(fusionTable.getServer());
			   if (!fusionTable.getServer().equals("server")){
			   data.addRowFromValues(fusionTable.getServer(), fusionTable.getAsrtimeout(),fusionTable.getCnNetworkTimeout(),fusionTable.getSample_failure_on_web_server(),
					   fusionTable.getWebsphere_issue_xml_timeout(),fusionTable.getTeleserver_rejected_calls(),
					   fusionTable.getAsrerrorresponse(),fusionTable.getCnHLMOutOfService(),fusionTable.getDefaultTrap());
			   }
			   
		  }
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (ServiceException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}catch (TypeMismatchException e) {
      System.out.println("Invalid type!");
    }
	  
    
    return data;
  }

  /**
   * NOTE: By default, this function returns true, which means that cross
   * domain requests are rejected.
   * This check is disabled here so examples can be used directly from the
   * address bar of the browser. Bear in mind that this exposes your
   * data source to xsrf attacks.
   * If the only use of the data source url is from your application,
   * that runs on the same domain, it is better to remain in restricted mode.
   */
  @Override
  protected boolean isRestrictedAccessMode() {
    return false;
  }
}
